package tk.mybatis.springboot.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.springboot.cons.AppCons;
import tk.mybatis.springboot.model.TActivity;
import tk.mybatis.springboot.service.ActivityService;
import tk.mybatis.springboot.util.DateUtils;
import tk.mybatis.springboot.util.RedisUtil;

import java.text.DateFormat;
import java.util.Calendar;

/**
 * Created by NightDream on 2017/9/15.
 */
@Component
public class SharkTask {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ActivityService activityService;

    @Scheduled(cron = "*/1 * * * * ?")
    private void process() {
        Long startTime = (Long) redisUtil.get(AppCons.REDIS_KEY_STARTTIME);
        Boolean isChangeStatus = (Boolean) redisUtil.get(AppCons.REDIS_KEY_STATUSCHANGED);
        if (startTime != null && isChangeStatus != null && startTime <= System.currentTimeMillis()) {
            if (!isChangeStatus) {
                log.info("活动开启...............");
                //如果到开始时间则变更活动状态
                TActivity activity = activityService.getTActivity(new TActivity(0));
                activity.setStatus("1");
                activityService.save(activity);
                redisUtil.set(AppCons.REDIS_KEY_STATUSCHANGED, true);
            }
        }
    }
}
